package com.ca.ridersclubapplication.fragment;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ridersclubapplication.MainActivity;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.request.AfterExecutor;
import com.ca.ridersclubapplication.request.MessageRequester;
import com.ca.ridersclubapplication.request.dto.AccountDto;
import com.ca.ridersclubapplication.request.dto.MsgListDto;
import com.ca.ridersclubapplication.request.dto.Result;
import com.ca.ridersclubapplication.service.MessageService;
import com.ca.ridersclubapplication.adapter.MessageAdapter;
import com.ca.ridersclubapplication.view.CallbackView;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment implements RefreshableFragment {

    private ConstraintLayout parentCL;
    private RecyclerView rv;
    private MessageAdapter adapter;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message_layout, container, false);

        initView(view);

        loadData();

        return view;
    }

    private void initView(View view) {

        parentCL = view.findViewById(R.id.cl_parent);

        rv = view.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MessageAdapter(getActivity(), null);
        rv.setAdapter(adapter);

        parentCL.setPadding(0, MainActivity.getTitleCLHeight() + 8, 0, 0);

        //  放入数据处理器中，等待回调
        DataHandler.putView("HasNewMsg", new CallbackView() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void update() {
                adapter.setList(MessageService.list);
                Log.e("新的消息列表", "" + MessageService.list);
            }
        });
    }

    private void loadData() {

        listMsgList();
    }

    private void listMsgList() {
        //  请求消息列表
        MessageRequester.listMsgList(getActivity(), new AfterExecutor<List<MsgListDto>>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void execute(Result<List<MsgListDto>> result) {
                List<MsgListDto> data = result.getData();
                if (result.isSuccess() && data != null) {
                    //  请求成功，添加数据
                    adapter.setList(data);
                    Log.e("首次请求的数据", data.toString());

                    //  请求完毕，关闭下拉刷新动画
                    MainActivity activity = (MainActivity) getActivity();
                    if (activity != null) {
                        activity.stopRefreshing();
                    }
                }
            }
        });
    }

    public void setReadAll() {
        if (adapter != null) {
            adapter.setReadAll();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void refresh() {
        if (adapter != null) {
            loadData();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void clearMsgList() {
        if (adapter != null) {
            adapter.setList(new ArrayList<>());
        }
    }
}
